package im.xz.cn.xiacore.tpPlayer;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;

public class tpp_accept implements Listener {
    @EventHandler
    public void qwq(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        String name = player.getName();
        boolean success = false;
        try {
            if (event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "传送快捷选择物")) {
                String targetName = event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getLore().get(2);
                try {
                    for (Object o : player.getServer().getOnlinePlayers().toArray()) {
                        Player awa = (Player) o;
                        if (awa.getName().toString().equalsIgnoreCase(targetName)) {
                            Player targetPlayer = (Player) o;
                            Location location = player.getLocation();
                            targetPlayer.teleport(location);
                            targetPlayer.sendMessage("传送成功！");
                            player.getInventory().getItemInMainHand().setAmount(0);
                            event.setCancelled(true);
                            success = true;
                            break;
                        }else {

                        }
                    }
                }catch (NullPointerException ignore) {
                }
                try {
                    if (!success) {
                        player.sendMessage(ChatColor.RED + "此玩家不存在或已下线");
                        player.getInventory().getItemInMainHand().setAmount(0);
                    }else {
                        player.sendMessage("传送成功！");

                    }
                }catch (NullPointerException ignore) {

                }
            }

        }catch (NullPointerException ignore) {

        }

    }
}